<template>
  <el-dialog
    draggable
    overflow
    :title="titleMap[mode]"
    v-model="visible"
    :width="900"
    destroy-on-close
    @closed="$emit('closed')"
  >
    <el-form :model="form" :rules="rules" ref="dialogForm" label-width="70px">
      <sk-tabs type="border-card">
        <el-tab-pane ref="tab" label="基础信息">
          <el-row>
            <el-col :span="6">
              <el-form-item label="单号" prop="code">
                <el-input v-model="form.code" disabled placeholder="自动生成"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="单据日期" prop="billdate">
                <el-date-picker
                  v-model="form.billdate"
                  type="date"
                  placeholder="选择日期"
                  format="YYYY-MM-DD"
                  value-format="YYYY-MM-DD"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" placeholder=""></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="匹" prop="auxiliary_quantity">
                <el-input
                  v-model="form.auxiliary_quantity"
                  @keyup="setQuantity"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="数" prop="quantity">
                <el-input v-model="form.quantity" @keyup="setAmount"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="单价" prop="price">
                <el-input v-model="form.price" @keyup="setAmount"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="金额" prop="amount">
                <el-input v-model="form.amount" @keyup="setPrice"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="运输单" prop="remark">
                <skAutoChoose
                  v-model="form.car.code"
                  :name="'car'"
                  :assign="form"
                  @select="selectCar"
                >
                </skAutoChoose>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="运输商" prop="remark">
                <el-row>
                  <el-col :span="12">
                    <skAutoChoose
                      v-model="form.car.company.name"
                      :name="'factory'"
                      :asKey="'company_id'"
                      :assign="form.car"
                      :params="[{ category_id: 54 }]"
                    >
                    </skAutoChoose>
                  </el-col>
                  <el-col :span="12">
                    <el-input
                      v-model="form.car.amount"
                      clearable
                      placeholder="运费"
                    ></el-input>
                  </el-col>
                </el-row>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="库位" prop="location_id">
                <skAutoChoose
                  v-model="form.location.name"
                  :name="'location'"
                  :assign="form"
                >
                </skAutoChoose>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="额外费用" prop="location_id">
                <el-input
                  v-model="form.extra_cost"
                  clearable
                  placeholder="额外费用"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <sc-VxeTable
            :tablecode="'pro_outsourcing_product_raws_save'"
            :data="form.raws"
            :gridOps="gridSubOpts"
            ref="subbe"
          >
          </sc-VxeTable>
          <sc-VxeTable
            :tablecode="'pro_outsourcing_product_save'"
            :data="form.details"
            :gridOps="gridSubOpts"
            ref="subbe"
          >
          </sc-VxeTable>
        </el-tab-pane>
        <el-tab-pane label="附件">
          <sk-upload
            v-model="form.attachments"
            :limit="10"
            draggable
            tip="单个文件不要超过10M,请上传图像格式文件"
          >
          </sk-upload>
        </el-tab-pane>
      </sk-tabs>
    </el-form>

    <!-- </sc-water-mark> -->
    <template #footer>
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script>
import skDicSelect from "@/components/skDicSelect";
import skAutoChoose from "@/components/skAutoChoose";
import XEUtils from "xe-utils";
export default {
  emits: ["success", "closed"],
  components: { skAutoChoose, skDicSelect },
  data() {
    return {
      mode: "add",
      form: {
        billdate: XEUtils.toDateString(new Date(), "yyyy-MM-dd"),
        material: {},
        outfactory: {},
        car: { company: {} },
        raws: [],
        details: [],
        attachments: [],
      },
      gridSubOpts: {
        height: 250,
        columns: [
          {
            field: "rate",
            event: {
              keyup: ({ row }) => {
                row.quantity = (this.form.quantity * row.rate) / 100;
              },
            },
          },
        ],
      },
      titleMap: {
        add: "新增",
        edit: "编辑",
      },
      rules: {
        bom_id: { required: true, message: "工艺必填" },
        type_id: { required: true, message: "形态必选" },
        outfactory_id: { required: true, message: "加工厂必选" },
        quantity: { required: true, message: "必须输入计划数量" },
        price: { required: true, message: "必须输入单价" },
        material_id: { required: true, message: "品名必填" },
        billdate: { required: true, message: "回收日期必填" },
      },
      visible: false,
      isSaveing: false,
    };
  },
  mounted() {},
  methods: {
    setQuantity() {
      this.setAmount();
    },

    setAmount() {
      this.form.quantity = this.$TOOL.isNumber(this.form.quantity)
        ? this.form.quantity
        : 0;
      this.form.price = this.$TOOL.isNumber(this.form.price) ? this.form.price : 0;
      this.form.amount = this.form.quantity * this.form.price;
      this.setMaterialQuantity();
    },
    setPrice() {
      let price = this.form.amount / this.form.quantity;
      this.form.price = this.$TOOL.isNumber(price) ? price : 0;
      this.form.quantity = this.$TOOL.isNumber(this.form.quantity)
        ? this.form.quantity
        : 0;
    },

    open(mode = "add") {
      this.mode = mode;
      this.visible = true;
      return this;
    },

    selectCar(data) {
      if (typeof data.code == "undefined") {
        data = { company: {} };
        this.form.car_id = 0;
      }
      this.form.car = data;
    },
    //表单提交方法
    submit() {
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          this.isSaveing = true;
          delete this.form.delivery_quantity;
          delete this.form.material_quantity;
          var res = await this.$API.pro.outsourcing.product.save.post(this.form);
          this.isSaveing = false;
          if (res.code == 200) {
            this.$emit("success", this.form);
            this.visible = false;
            this.$message.success("操作成功");
          }
        }
      });
    },
    setMaterialQuantity() {
      this.form.raws = this.form.raws.map((v) => {
        v.quantity = (this.form.quantity * v.rate) / 100;
        return v;
      });
      this.form.details = this.form.details.map((v) => {
        v.quantity = (this.form.quantity * v.rate) / 100;
        return v;
      });
    },
    //表单注入数据
    setData(data, selectRecord) {
      Object.assign(this.form, this.$TOOL.objCopy(this.$TOOL.removeNull(data)));
      //清除ID
      delete this.form.id;
      this.form.raws = this.form.raws.map((v) => {
        v.quantity = (this.form.quantity * v.rate) / 100;
        v.delivery_id = v.id;
        delete v.id;
        return v;
      });
      this.form.details = this.form.details.map((v) => {
        v.quantity = (this.form.quantity * v.rate) / 100;
        v.delivery_id = v.id;
        delete v.id;
        return v;
      });
      selectRecord && Object.assign(this.form, this.$TOOL.removeNull(selectRecord));
    },
  },
};
</script>

<style>
.attachments .el-upload--picture-card,
.attachments .el-upload-list--picture-card .el-upload-list__item {
  width: 100px;
  height: 100px;
  overflow: hidden;
}
</style>
